/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/**
 * 小程序功能管理-绑单扣款
 */
import React, { useEffect, useRef, useState } from 'react';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import KbBindModal from '../components/KbBindModal/index';
import { message } from 'antd';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import { TableItem } from '../_utils/index.d';
import { getColumns } from '@/components/_pages/mini/yjBindDeduction/_utils/index.config';
import { getWkdAfterPayList } from '@/services/mini/yjBindDeduction';

interface BindDeduction {
  type: string;
  show: boolean;
  activeKey: string;
}
const Index = ({ show, type, activeKey }: BindDeduction) => {
  const actionRef = useRef<ActionType>();
  const [record, setRecord] = useState<TableItem>({});
  const [visible, setVistble] = useState<boolean>(false);
  const queryTable = async (param: { current: string; pageSize: string }) => {
    const activeList = {
      //0 待处理 1 已处理
      bindDeduction: '0',
      deductionLog: '1',
    };
    const { current: page, pageSize } = param;
    const { code, data, msg } = await getWkdAfterPayList({
      page,
      pageSize,
      status: activeList[activeKey],
    });
    if (code == 0) {
      return {
        data: data.lists,
        total: data.total,
      };
    } else {
      message.error(msg);
      return {};
    }
  };

  const handleModalClose = () => setVistble(false);
  const handleOpenModal = (data: TableItem) => {
    setRecord(data);
    setVistble(true);
  };
  const optionColumns: ProColumns<TableItem>[] = [
    {
      title: '操作',
      valueType: 'option',
      align: 'center',
      width: 80,
      fixed: 'right',
      render: (_, record_) => {
        const { status } = record_;
        return (
          <a onClick={handleOpenModal.bind(null, record_)}>
            {status == '2' ? '查看结果' : '绑单扣款'}
          </a>
        );
      },
    },
  ];

  useEffect(() => {
    if (!show || type !== activeKey) return;
    actionRef.current?.reload();
  }, [show, type, activeKey]);
  return (
    <DragTable>
      <ProTable
        rowKey='order_id'
        actionRef={actionRef}
        request={queryTable}
        columns={[...getColumns(activeKey), ...optionColumns]}
        options={false}
        scroll={{ x: 900 }}
        search={false}
        pagination={{
          pageSize: 20,
          showSizeChanger: false,
        }}
      />
      <KbBindModal
        visible={visible}
        actionRef={actionRef}
        onClose={handleModalClose}
        item={record}
      />
    </DragTable>
  );
};

export default Index;
